/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiElements.java
 *
 * Created on 05.11.2012, 13:06:42
 */
package client;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.*;
import static client.RefManager.*;
import java.util.Collections;

/**
 *
 * @author abd260
 */
public class GuiElements extends javax.swing.JFrame {

    /**
     * Creates new form GuiElements
     */
    public GuiElements() {
        initComponents();
        this.setVisible(true);
        this.startConfig();
    }

    //anfangsaussehen der gui, und beim neusarten einer vebindung
    void startConfig() {
        AbsendenButton.setEnabled(false);
        ChatTeilnehmerLabel.setEnabled(false);
        ChatTeilnehmerListe.setEnabled(false);
        EmpfangeneNachrichtenTextFeld.setEnabled(false);
        SchreibeNachrichtLabel.setEnabled(false);
        SchreibeNachrichtenTextPane.setEnabled(false);
        HostnameTextFeld.setText("");
        UsernameTextFeld.setText("");
        HostnameTextFeld.setEnabled(true);
        HostnameLabel.setEnabled(true);
        UsernameTextFeld.setEnabled(true);
        UsernameLabel.setEnabled(true);
    }

    //empfangene Nachrichten in das korrespondierende TextFeld
    void showMessageInGUI(final String message) {
        
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                String bisherImFeld = EmpfangeneNachrichtenTextFeld.getText();
                bisherImFeld += message;
                EmpfangeneNachrichtenTextFeld.setText(bisherImFeld);
            }
        });
        
    }

    //wenn der login == true, aussehen der gui
    void loggedInVisibilityForFrames() {
        AbsendenButton.setEnabled(true);
        ChatTeilnehmerLabel.setEnabled(true);
        ChatTeilnehmerListe.setEnabled(true);
        EmpfangeneNachrichtenTextFeld.setEnabled(true);
        SchreibeNachrichtLabel.setEnabled(true);
        SchreibeNachrichtenTextPane.setEnabled(true);
        HostnameTextFeld.setEnabled(false);
        HostnameLabel.setEnabled(false);
        UsernameTextFeld.setEnabled(false);
        UsernameLabel.setEnabled(false);
    }
    
    //FehlerFenster, generisch
    public void showError(String message, String title) {
        JOptionPane.showMessageDialog(this, message, title, ERROR_MESSAGE);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MainPanel = new javax.swing.JPanel();
        SchreibeNachrichtLabel = new javax.swing.JLabel();
        AbsendenButton = new javax.swing.JButton();
        ChatTeilnehmerLabel = new javax.swing.JLabel();
        ChatTeilnehmerPane = new javax.swing.JScrollPane();
        ChatTeilnehmerListe = new javax.swing.JList();
        UsernameLabel = new javax.swing.JLabel();
        UsernameTextFeld = new javax.swing.JTextField();
        SchreibeNachrichtenPane = new javax.swing.JScrollPane();
        SchreibeNachrichtenTextPane = new javax.swing.JTextPane();
        EinloggenButton = new javax.swing.JButton();
        HostnameLabel = new javax.swing.JLabel();
        HostnameTextFeld = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        EmpfangeneNachrichtenTextFeld = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Dayo Chat Communications");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        SchreibeNachrichtLabel.setText("Schreiben Sie Ihre Nachricht:");

        AbsendenButton.setText("Absenden");
        AbsendenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AbsendenButtonActionPerformed(evt);
            }
        });

        ChatTeilnehmerLabel.setText("Chat-Teilnehmer:");

        ChatTeilnehmerPane.setViewportView(ChatTeilnehmerListe);

        UsernameLabel.setText("Username");

        UsernameTextFeld.setText("Username");

        SchreibeNachrichtenTextPane.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                SchreibeNachrichtenTextPaneKeyReleased(evt);
            }
        });
        SchreibeNachrichtenPane.setViewportView(SchreibeNachrichtenTextPane);

        EinloggenButton.setText("Einloggen");
        EinloggenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EinloggenButtonActionPerformed(evt);
            }
        });

        HostnameLabel.setText("Hostname");

        HostnameTextFeld.setText("Hostname");

        jScrollPane1.setViewportView(EmpfangeneNachrichtenTextFeld);

        javax.swing.GroupLayout MainPanelLayout = new javax.swing.GroupLayout(MainPanel);
        MainPanel.setLayout(MainPanelLayout);
        MainPanelLayout.setHorizontalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
                    .addGroup(MainPanelLayout.createSequentialGroup()
                        .addComponent(UsernameLabel)
                        .addGap(10, 10, 10)
                        .addComponent(UsernameTextFeld, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(HostnameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(HostnameTextFeld, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(EinloggenButton))
                    .addComponent(AbsendenButton)
                    .addGroup(MainPanelLayout.createSequentialGroup()
                        .addComponent(SchreibeNachrichtLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(SchreibeNachrichtenPane, javax.swing.GroupLayout.PREFERRED_SIZE, 496, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(24, 24, 24)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ChatTeilnehmerLabel)
                    .addComponent(ChatTeilnehmerPane, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
                .addContainerGap())
        );
        MainPanelLayout.setVerticalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(EinloggenButton)
                    .addComponent(HostnameTextFeld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(HostnameLabel)
                    .addComponent(UsernameTextFeld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(UsernameLabel)
                    .addComponent(ChatTeilnehmerLabel))
                .addGap(16, 16, 16)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MainPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(SchreibeNachrichtLabel)
                            .addComponent(SchreibeNachrichtenPane, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(AbsendenButton))
                    .addComponent(ChatTeilnehmerPane))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Click -> Login, macht eine neue ServerConnection auf
private void EinloggenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EinloggenButtonActionPerformed
    String username = UsernameTextFeld.getText().trim();
    String hostname = HostnameTextFeld.getText().trim();
    thread = new ServerConnectionThread(hostname, username);
    thread.start();
}//GEN-LAST:event_EinloggenButtonActionPerformed
    //für den x-Button 
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if(thread != null)
        thread.interrupt();
    }//GEN-LAST:event_formWindowClosing
    //hier wird ein neuer NachrichtenThread erstellt und mit einer Nachricht empfangen
    private void AbsendenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AbsendenButtonActionPerformed
        String nachricht = SchreibeNachrichtenTextPane.getText().trim();
        if (nachricht.length() > 100) {
            this.showError("Nachricht zu lang", "Fehlerhafte Eingabe");
        } else {
            new NachrichtSendenThread(nachricht).start();
            SchreibeNachrichtenTextPane.setText("");
        }
    }//GEN-LAST:event_AbsendenButtonActionPerformed
    //Enter für absenden
    private void SchreibeNachrichtenTextPaneKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SchreibeNachrichtenTextPaneKeyReleased
        if (evt.getKeyCode() == evt.VK_ENTER) //this.AbsendenButtonActionPerformed(null);
        {
            AbsendenButton.doClick();
        }
    }//GEN-LAST:event_SchreibeNachrichtenTextPaneKeyReleased
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AbsendenButton;
    private javax.swing.JLabel ChatTeilnehmerLabel;
    private javax.swing.JList ChatTeilnehmerListe;
    private javax.swing.JScrollPane ChatTeilnehmerPane;
    private javax.swing.JButton EinloggenButton;
    private javax.swing.JTextPane EmpfangeneNachrichtenTextFeld;
    private javax.swing.JLabel HostnameLabel;
    private javax.swing.JTextField HostnameTextFeld;
    private javax.swing.JPanel MainPanel;
    private javax.swing.JLabel SchreibeNachrichtLabel;
    private javax.swing.JScrollPane SchreibeNachrichtenPane;
    private javax.swing.JTextPane SchreibeNachrichtenTextPane;
    private javax.swing.JLabel UsernameLabel;
    private javax.swing.JTextField UsernameTextFeld;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    //zieht sich userlist aus dem client
    void redrawUserList() {
        List<String> userListe = new ArrayList<String>(client.userMap.values());
        Collections.sort(userListe);
        ChatTeilnehmerListe.clearSelection();
        ChatTeilnehmerListe.setListData(userListe.toArray());
    }
}
